/********************************************************************************
    Author :
********************************************************************************/

/********************************************************************************
    Include Files :
********************************************************************************/
#include "extern.h"
    #include "sdk_project_config.h"
    #include "interrupt_manager.h"

#include "alloc.h"
    #include "FTM.h"
/********************************************************************************
    Defination :
********************************************************************************/
/********************************************************************************
    Comment : The defination of time counter
********************************************************************************/
#define TIMER_TIME_OUT   		  (uint8)0
#define UINT8_TIMER_10MS_COUNTER  (uint8)10
#define UINT8_TIMER_100MS_COUNTER (uint8)100

/********************************************************************************
    Type name :
********************************************************************************/

/********************************************************************************
    Comment : The description of type name
********************************************************************************/

/********************************************************************************
    Private variables :
********************************************************************************/

/********************************************************************************
    Comment : The time counter of 10ms and 100ms
********************************************************************************/
PRIVATE uint8 u8Sw10msTimer = TIMER_TIME_OUT;
PRIVATE uint8 u8Sw100msTimer = TIMER_TIME_OUT;

/********************************************************************************
    Private function declaration :
********************************************************************************/
PRIVATE FUNC(void,FTM_CODE) vidFTMStandardFunction(void);
/********************************************************************************
    Public function :
********************************************************************************/

/********************************************************************************
    Description :
        The initial function of FTM
********************************************************************************/
PUBLIC FUNC(void,FTM_CODE) FTM_vidInitFunction(void)
{
	/* Write your local variable definition here */
	ftm_state_t state;

    /* Initialize FTM */
    FTM_DRV_Init(INST_FLEXTIMER_MC_1, &flexTimer_mc_1_InitConfig_0, &state);

    INT_SYS_InstallHandler(FTM1_Ovf_Reload_IRQn,&vidFTMStandardFunction,NULL);

    /* Initialize counter */
    FTM_DRV_InitCounter(INST_FLEXTIMER_MC_1, &flexTimer_mc_1_TimerConfig_0);

    /* Start Counter */
    FTM_DRV_CounterStart(INST_FLEXTIMER_MC_1);
}

/********************************************************************************
    Description :
        Check if 10ms time out
********************************************************************************/
PUBLIC FUNC(boolean,FTM_CODE) FTM_b10msTimeout(void)
{
    boolean bTimeout = FALSE;
    if(u8Sw10msTimer == UINT8_TIMER_10MS_COUNTER)
    {
        bTimeout = TRUE;
    }
    else
    {
        
    }
    return bTimeout;
}

/********************************************************************************
    Description :
        Check if 100ms time out
********************************************************************************/
PUBLIC FUNC(boolean,FTM_CODE) FTM_b100msTimeout(void)
{
    boolean bTimeout = FALSE;
    if(u8Sw100msTimer == UINT8_TIMER_100MS_COUNTER)
    {
        bTimeout = TRUE;
    }
    else
    {
        
    }
    return bTimeout;
}

/********************************************************************************
    Description :
        Reset 10ms timer counter
********************************************************************************/
PUBLIC FUNC(void,FTM_CODE) FTM_vidClear10msTimerFlag(void)
{
    u8Sw10msTimer = 0;
}

/********************************************************************************
    Description :
        Reset 100ms timer counter
********************************************************************************/
PUBLIC FUNC(void,FTM_CODE) FTM_vidClear100msTimerFlag(void)
{
    u8Sw100msTimer = 0;
}


/********************************************************************************
    Private function :
********************************************************************************/

/********************************************************************************
    Description :
        The description of this function
********************************************************************************/

PRIVATE FUNC(void,FTM_CODE) vidFTMStandardFunction(void)
{
    if(u8Sw10msTimer < UINT8_TIMER_10MS_COUNTER)
    {
        u8Sw10msTimer++;
    }
    else
    {
        
    }
    if(u8Sw100msTimer < UINT8_TIMER_100MS_COUNTER)
    {
        u8Sw100msTimer++;
    }
    else
    {
        
    }

    FTM_DRV_ClearStatusFlags(INST_FLEXTIMER_MC_1, (uint32_t)FTM_TIME_OVER_FLOW_FLAG);
}
